package com.dwxt.community.service;

import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.IDUtils;
import com.dwxt.common.util.PartyUtil;
import com.dwxt.community.dao.CustomerProblemMapper;
import com.dwxt.community.entity.CustomerProblem;
import com.dwxt.community.entity.CustomerProblemExample;
import com.dwxt.community.service.feign.SocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService {
    @Autowired
    private CustomerProblemMapper customerProblemMapper;

    @Autowired
    private SocketService socketService;

    public void commitProblem(CustomerProblem customerProblem) {
        customerProblem.setUserId(PartyUtil.getCurrentUserId());
        customerProblem.setIsHandle(YesOrNo.NO);
        customerProblem.setConversationId(IDUtils.genItemId());
        customerProblem.setIsProblem(YesOrNo.YES);
        customerProblemMapper.insertSelective(customerProblem);
        socketService.reply("community", customerProblem.getId().toString(), null);
    }

    public List<CustomerProblem> selectProblemList() {
        CustomerProblemExample example = new CustomerProblemExample();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andUserIdEqualTo(PartyUtil.getCurrentUserId()).andIsProblemEqualTo(YesOrNo.YES);
        return customerProblemMapper.selectByExampleWithBLOBs(example);
    }

    public void delete(Integer id) {
        customerProblemMapper.deleteByPrimaryKey(id);
    }

    public List<CustomerProblem> selectProblemDetail(String conversationId) {
        CustomerProblemExample example = new CustomerProblemExample();
        example.setOrderByClause("create_time");
        example.createCriteria().andConversationIdEqualTo(conversationId);
        return customerProblemMapper.selectByExampleWithBLOBs(example);
    }
}
